package application;

import java.util.ArrayList;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Story{
	
	public String Name;
	public String Description;
	public Author StoryAuthor;


	public Story (
			String Name, 
			String Description, 
			Author StoryAuthor
			){
		
		super();
		this.Name = Name;
		this.Description = Description;
		this.StoryAuthor = StoryAuthor;
	}
	
	public Story(){
		super();
		this.Name = "new story";
		this.Description = "this is your story";
		this.StoryAuthor = new Author();
	}
	
	public void dbEditStory (String dbFile, Story c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Story> result = db
		        .queryByExample(Story.this);
				Story found = (Story) result.next();
				db.delete(found);
				this.setDescription(c.getDescription());
				this.setName(c.getName());
				this.setStoryAuthor(c.getStoryAuthor());
				
				db.store(this);
				
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbEditAuthor (String dbFile, Author c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Story> result = db
		        .queryByExample(Story.this);
				Story found = (Story) result.next();
				System.out.println("story found");
				db.delete(found);
				System.out.println("story deleted");
				this.setStoryAuthor(c);
				System.out.println("story updated");
				db.store(this);
				
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbCreateStory (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}
	
	public void dbDeleteStory (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Story> result = db
		        .queryByExample(Story.this);
				Story found = (Story) result.next();
				db.delete(found);
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void chapterNrUp(ArrayList<Chapter> l, Chapter tmp){
		int nr = tmp.getNr();
		int index = l.indexOf(tmp);
		
		for(int i=0;i<l.size();i++){
			Chapter c=l.get(i);
			if(c.equals(tmp)){
				index = l.indexOf(c);
				c.setNr(nr-1);
				l.set(index, c);
				Chapter o = l.get(index -1);
				o.setNr(nr);
				l.set(index -1, o);
			}
		}
	}
	
	public void chapterNrDown(ArrayList<Chapter> l, Chapter tmp){
		int nr = tmp.getNr();
		int index = l.indexOf(tmp);
		
		for(int i=0;i<l.size();i++){
			Chapter c=l.get(i);
			if(c.equals(tmp)){
				index = l.indexOf(c);
				c.setNr(nr+1);
				l.set(index, c);
				Chapter o = l.get(index +1);
				o.setNr(nr);
				l.set(index +1, o);
			}
		}
	}
	
	///////////////////
	// Getter & Setter:
	///////////////////

	public String getName() {
		return Name;
	}

	public String getDescription() {
		return Description;
	}

	public Author getStoryAuthor() {
		return StoryAuthor;
	}


	public void setName(String name) {
		Name = name;
	}

	public void setDescription(String description) {
		Description = description;
	}

	public void setStoryAuthor(Author storyAuthor) {
		StoryAuthor = storyAuthor;
	}

	public String toString() {
		return Name;
	}
	

	
	
}
